<?php
class MysqlDber
{
	protected $db;
	protected $uId;
	
	/**
	 * 获取数据库单例
	 * @param	int $uId	用户ID
	 * @return	iDber
	 */
	public static function & getInstance( $uId )
	{
		static $dbObject = array();
		if( !isset( $dbObject[$uId] ) )
		{
			$dbObject[$uId] = new MysqlDber( $uId );
		}
		
		return $dbObject[$uId];
	}
	
	/**
	 * 实例化数据库类
	 *
	 * @param String $dbName	//数据库名
	 */
	protected function __construct( $uId )
	{
		$this->uId = $uId;
		$dbConfig = & Common::getConfig( 'mysqlDb' );
		$this->db = new MysqlDb( $dbConfig['game'] );
	}
	
	/**
	 * 数据新增接口
	 * @param	string $tableName		数据表名
	 * @param	array $value			数据
	 * @param	array $condition		条件:array( 'id' => 1 )
	 * @return	boolean
	 */
	public function add( $tableName , $value , $condition = array() )
	{
		$value = $value + $condition;
		//$value['uid'] = $this->uId;
		$keys = array_keys( $value );
		$sql = "INSERT INTO `{$tableName}` (`" . implode( "` , `" , $keys ) . "`) VALUES (\"" . implode( '" , "' , $value ) . "\")";
		$this->db->query( $sql );
		return $this->db->affectedRows() ? $this->db->insertId() : false;
	}
	
	/**
	 * 数据修改接口
	 * @param	string $tableName		数据表名
	 * @param	array $value			数据
	 * @param	array $condition		条件:array( 'id' => 1 )
	 * @return	boolean
	 */
	public function update( $tableName , $value , $condition = array() )
	{
		$sql = "UPDATE `{$tableName}` SET ";
		foreach ( $value as $key => $item )
		{
			$sql .= "`{$key}` = '{$item}',";
		}
		
		$sql .= "`uid` = {$this->uId} WHERE `uid` = {$this->uId}";
		foreach ( $condition as $key => $item )
		{
			$sql .= " AND `{$key}` = '{$item}'";   
			
			
		}
		
		$this->db->query( $sql );
		return $this->db->affectedRows();
	}
	
	/**
	 * 数据删除接口
	 * @param	string $tableName		数据表名
	 * @param	array $condition		条件:array( 'id' => 1 )
	 * @return	boolean
	 */
	public function delete( $tableName , $condition = array() )
	{
		$sql = "DELETE FROM `{$tableName}` WHERE `uid` = {$this->uId}";
		foreach ( $condition as $key => $item )
		{
			$sql .= " AND `{$key}` = '{$item}'";   
		}
		$this->db->query( $sql );
		return $this->db->affectedRows();
	}
	
	/**
	 * 数据单项查询接口(只能根据用户ID查询)
	 * @param	string $tableName		数据表名
	 * @param	array $value			数据
	 * @return	array
	 */
	public function find( $tableName , $condition = null )
	{
		if ( $condition == null )
		{
			$sql = "SELECT * FROM `{$tableName}` WHERE `uid` = {$this->uId} ";
		}
		else if ( is_array( $condition ) )
		{
			$sql = "SELECT * FROM `{$tableName}` WHERE ";
			$i = 0;
			foreach ( $condition as $key => $val ) 
			{
				$i++;
				$sql .= "`{$key}` = '{$val}' ";
				if ( $i != count( $condition ) )
				{
					$sql .= " and ";
				}
			}
		}
		else 
		{
			$sql = "SELECT * FROM `{$tableName}` WHERE ". $condition;
		}
		$sql .= " LIMIT 1";
		return $this->db->fetchOneAssoc( $sql );
	}
	
	
	/**
	 * 数据多项查询接口
	 *
	 * @param	string $tableName		数据表名
	 * @param	array $returnItems		需要的字段
	 * @return	array
	 */
	public function findAll( $tableName , $returnItems = "*" , $condition = '' )
	{
		if ( is_array( $returnItems ) )
		{
			foreach ( $returnItems as $value ) 
			{
				$return = " `{$value}` ,";
			}
			$return = substr( $return , 0 , -1 );
		}
		else 
		{
			$return = $returnItems;	
		}
		if ( !empty( $condition ) )
		{
			if ( is_array( $condition ) )
			{
				$i = 0;
				foreach ( $condition as $key => $value ) 
				{
					$i++;
					$where .= " `{$key}` = '{$value}' "; 
					if ( $i < count( $condition ) )
					{
						$where .= "  && ";
					}
				}
			}
			else 
			{
				$where = $condition;
			}
		}
		$sql = "SELECT {$return} FROM `{$tableName}` WHERE {$where} ";
		return $this->db->fetchArray( $sql );
	}
	
	/**
	 * 全局数据ID获取接口
	 *
	 * @param	string $tableName		数据表名
	 * @return	int
	 */
	public function getID( $tableName )
	{
		return $this->db->get_id( $tableName );
	}
	
	public function getLastSql()
	{
		return $this->db->getLastSql();
	}
}
